﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/12/16 15:14:19
 * ************************************************************/
namespace Mozlite
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using System.Data;

    /// <summary>
    /// 类型转换扩展方法类。
    /// </summary>
    public static class Converters
    {
        /// <summary>
        /// 将字符串类型转换为<typeparamref name="T"/>类型。
        /// </summary>
        /// <typeparam name="T">转换后的类型。</typeparam>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">默认值，即转换失败后返回的值。</param>
        /// <returns>返回转换后的实例对象。</returns>
        public static T ChangeTo<T>(this object value, T defaultValue = default(T))
        {
            if (value == null)
                return defaultValue;
            Type type = typeof(T);
            Type nullableType = Nullable.GetUnderlyingType(type);
            if (nullableType != null)
                type = nullableType;
            try
            {
                switch (Type.GetTypeCode(type))
                {
                    case TypeCode.Boolean:
                        value = Convert.ToBoolean(value);
                        break;
                    case TypeCode.Byte:
                        value = Convert.ToByte(value);
                        break;
                    case TypeCode.Char:
                        value = Convert.ToChar(value);
                        break;
                    case TypeCode.DateTime:
                        value = Convert.ToDateTime(value);
                        break;
                    case TypeCode.Decimal:
                        value = Convert.ToDecimal(value);
                        break;
                    case TypeCode.Double:
                        value = Convert.ToDouble(value);
                        break;
                    case TypeCode.Int16:
                        value = Convert.ToInt16(value);
                        break;
                    case TypeCode.Int32:
                        value = Convert.ToInt32(value);
                        break;
                    case TypeCode.Int64:
                        value = Convert.ToInt64(value);
                        break;
                    case TypeCode.SByte:
                        value = Convert.ToSByte(value);
                        break;
                    case TypeCode.Single:
                        value = Convert.ToSingle(value);
                        break;
                    case TypeCode.String:
                        value = Convert.ToString(value);
                        break;
                    case TypeCode.UInt16:
                        value = Convert.ToUInt16(value);
                        break;
                    case TypeCode.UInt32:
                        value = Convert.ToUInt32(value);
                        break;
                    case TypeCode.UInt64:
                        value = Convert.ToUInt64(value);
                        break;
                    case TypeCode.Empty:
                    case TypeCode.DBNull:
                        return defaultValue;
                    default:
                        if (type == typeof(Guid))
                            value = value.ToGuid((Guid)(object)defaultValue);
                        else if (typeof(T) == typeof(Encoding))
                            value = value.ToEncoding((Encoding)(object)defaultValue);
                        break;
                }
                return (T)value;
            }
            catch
            {
                if (type.IsEnum)
                {
                    try
                    {
                        return (T)Enum.Parse(type, value as string, true);
                    }
                    catch { }
                }
                return defaultValue;
            }
        }
      
        /// <summary>
        /// 将文本字符串转换为枚举类型。
        /// </summary>
        /// <typeparam name="T">枚举类型。</typeparam>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">如果转换失败，返回的默认类型。</param>
        /// <returns>返回枚举类型实例。</returns>
        public static T ToEnum<T>(this object value, T defaultValue = default(T)) where T : struct
        {
            try
            {
                T retValue;
                if (Enum.TryParse<T>(value as string, true, out retValue))
                    return retValue;
            }
            catch { }
            return defaultValue;
        }
      
        /// <summary>
        /// 将字符串转换为Int32类型。
        /// </summary>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static int ToInt32(this object value, int defaultValue = default(int))
        {
            try { return Convert.ToInt32(value); }
            catch { }
            return defaultValue;
        }

        /// <summary>
        /// 将字符串转换为Int64类型。
        /// </summary>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static long ToInt64(this object value, long defaultValue = default(long))
        {
            try { return Convert.ToInt64(value); }
            catch { }
            return defaultValue;
        }
      
        /// <summary>
        /// 将字符串转换为Int16类型。
        /// </summary>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static short ToInt16(this object value, short defaultValue = default(short))
        {
            try { return Convert.ToInt16(value); }
            catch { }
            return defaultValue;
        }
      
        /// <summary>
        /// 将字符串转换为UInt32类型。
        /// </summary>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static uint ToUInt32(this object value, uint defaultValue = default(uint))
        {
            try { return Convert.ToUInt32(value); }
            catch { }
            return defaultValue;
        }
      
        /// <summary>
        /// 将字符串转换为UInt64类型。
        /// </summary>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static ulong ToUInt64(this object value, ulong defaultValue = default(ulong))
        {
            try { return Convert.ToUInt64(value); }
            catch { }
            return defaultValue;
        }
       
        /// <summary>
        /// 将字符串转换为UInt16类型。
        /// </summary>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static ushort ToUInt16(this object value, ushort defaultValue = default(ushort))
        {
            try { return Convert.ToUInt16(value); }
            catch { }
            return defaultValue;
        }
      
        /// <summary>
        /// 将字符串转换为布尔类型。
        /// </summary>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static bool ToBoolean(this object value, bool defaultValue = false)
        {
            try { return Convert.ToBoolean(value); }
            catch { }
            return defaultValue;
        }

        /// <summary>
        /// 将字符串转换为日期类型。
        /// </summary>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static DateTime ToDateTime(this object value, DateTime defaultValue = default(DateTime))
        {
            try { return Convert.ToDateTime(value); }
            catch { }
            return defaultValue;
        }
       
        /// <summary>
        /// 将字符串转换为decimal类型。
        /// </summary>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static decimal ToDecimal(this object value, decimal defaultValue = default(decimal))
        {
            try { return Convert.ToDecimal(value); }
            catch { }
            return defaultValue;
        }
       
        /// <summary>
        /// 将字符串转换为double类型。
        /// </summary>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static double ToDouble(this object value, double defaultValue = default(double))
        {
            try { return Convert.ToDouble(value); }
            catch { }
            return defaultValue;
        }
       
        /// <summary>
        /// 将字符串转换为float类型。
        /// </summary>
        /// <param name="value">用于转换的对象。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static float ToSingle(this object value, float defaultValue = default(float))
        {
            try { return Convert.ToSingle(value); }
            catch { }
            return defaultValue;
        }

        /// <summary>
        /// 转换为GUID。
        /// </summary>
        /// <param name="value">要转换的对象实例。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回GUID实例对象。</returns>
        public static Guid ToGuid(this object value, Guid defaultValue = default(Guid))
        {
            try
            {
                Guid retValue;
                if (Guid.TryParse(value as string, out retValue))
                    return retValue;
            }
            catch { }
            return defaultValue;
        }

        /// <summary>
        /// 获取编码。
        /// </summary>
        /// <param name="value">编码对象。</param>
        /// <param name="encoding">编码默认值。</param>
        /// <returns>返回编码实例对象。</returns>
        public static Encoding ToEncoding(this object value, Encoding defaultValue = null)
        {
            if (defaultValue == null)
                defaultValue = Encoding.UTF8;
            try
            {
                return Encoding.GetEncoding(value as string);
            }
            catch { }
            return defaultValue;
        }

        /// <summary>
        /// 将字符串类型转换为<typeparamref name="T"/>类型。
        /// </summary>
        /// <typeparam name="T">转换后的类型。</typeparam>
        /// <param name="value">用于转换的字符串。</param>
        /// <param name="defaultValue">默认值，即转换失败后返回的值。</param>
        /// <returns>返回转换后的实例对象。</returns>
        public static T ChangeTo<T>(this string value, T defaultValue = default(T))
        {
            if (value == null)
                return defaultValue;
            Type type = typeof(T);
            Type nullableType = Nullable.GetUnderlyingType(type);
            if (nullableType != null)
                type = nullableType;
            try
            {
                object retValue = defaultValue;
                switch (Type.GetTypeCode(type))
                {
                    case TypeCode.Boolean:
                        retValue = Convert.ToBoolean(value);
                        break;
                    case TypeCode.Byte:
                        retValue = Convert.ToByte(value);
                        break;
                    case TypeCode.Char:
                        retValue = Convert.ToChar(value);
                        break;
                    case TypeCode.DateTime:
                        retValue = Convert.ToDateTime(value);
                        break;
                    case TypeCode.Decimal:
                        retValue = Convert.ToDecimal(value);
                        break;
                    case TypeCode.Double:
                        retValue = Convert.ToDouble(value);
                        break;
                    case TypeCode.Int16:
                        retValue = Convert.ToInt16(value);
                        break;
                    case TypeCode.Int32:
                        retValue = Convert.ToInt32(value);
                        break;
                    case TypeCode.Int64:
                        retValue = Convert.ToInt64(value);
                        break;
                    case TypeCode.SByte:
                        retValue = Convert.ToSByte(value);
                        break;
                    case TypeCode.Single:
                        retValue = Convert.ToSingle(value);
                        break;
                    case TypeCode.String:
                        retValue = Convert.ToString(value);
                        break;
                    case TypeCode.UInt16:
                        retValue = Convert.ToUInt16(value);
                        break;
                    case TypeCode.UInt32:
                        retValue = Convert.ToUInt32(value);
                        break;
                    case TypeCode.UInt64:
                        retValue = Convert.ToUInt64(value);
                        break;
                    case TypeCode.Empty:
                    case TypeCode.DBNull:
                        return defaultValue;
                    default:
                        if (type == typeof(Guid))
                            retValue = value.ToGuid((Guid)(object)defaultValue);
                        else if (typeof(T) == typeof(Encoding))
                            retValue = value.ToEncoding((Encoding)(object)defaultValue);
                        break;
                }
                return (T)retValue;
            }
            catch
            {
                if (type.IsEnum)
                {
                    try
                    {
                        return (T)Enum.Parse(type, value, true);
                    }
                    catch { }
                }
                return defaultValue;
            }
        }

        /// <summary>
        /// 将文本字符串转换为枚举类型。
        /// </summary>
        /// <typeparam name="T">枚举类型。</typeparam>
        /// <param name="value">枚举字符串。</param>
        /// <param name="defaultValue">如果转换失败，返回的默认类型。</param>
        /// <returns>返回枚举类型实例。</returns>
        public static T ToEnum<T>(this string value, T defaultValue = default(T)) where T : struct
        {
            T retValue;
            if (Enum.TryParse<T>(value, true, out retValue))
                return retValue;
            return defaultValue;
        }
       
        /// <summary>
        /// 将字符串转换为Int32类型。
        /// </summary>
        /// <param name="value">字符串。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static int ToInt32(this string value, int defaultValue = default(int))
        {
            int v;
            if (int.TryParse(value, out v))
                return v;
            return defaultValue;
        }
      
        /// <summary>
        /// 将字符串转换为Int64类型。
        /// </summary>
        /// <param name="value">字符串。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static long ToInt64(this string value, long defaultValue = default(long))
        {
            long v;
            if (long.TryParse(value, out v))
                return v;
            return defaultValue;
        }
       
        /// <summary>
        /// 将字符串转换为Int16类型。
        /// </summary>
        /// <param name="value">字符串。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static short ToInt16(this string value, short defaultValue = default(short))
        {
            short v;
            if (short.TryParse(value, out v))
                return v;
            return defaultValue;
        }
       
        /// <summary>
        /// 将字符串转换为UInt32类型。
        /// </summary>
        /// <param name="value">字符串。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static uint ToUInt32(this string value, uint defaultValue = default(uint))
        {
            uint v;
            if (uint.TryParse(value, out v))
                return v;
            return defaultValue;
        }
      
        /// <summary>
        /// 将字符串转换为UInt64类型。
        /// </summary>
        /// <param name="value">字符串。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static ulong ToUInt64(this string value, ulong defaultValue = default(ulong))
        {
            ulong v;
            if (ulong.TryParse(value, out v))
                return v;
            return defaultValue;
        }

        /// <summary>
        /// 将字符串转换为UInt16类型。
        /// </summary>
        /// <param name="value">字符串。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static ushort ToUInt16(this string value, ushort defaultValue = default(ushort))
        {
            ushort v;
            if (ushort.TryParse(value, out v))
                return v;
            return defaultValue;
        }
      
        /// <summary>
        /// 将字符串转换为布尔类型。
        /// </summary>
        /// <param name="value">字符串。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static bool ToBoolean(this string value, bool defaultValue = false)
        {
            bool v;
            if (bool.TryParse(value, out v))
                return v;
            return defaultValue;
        }
      
        /// <summary>
        /// 将字符串转换为日期类型。
        /// </summary>
        /// <param name="value">字符串。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static DateTime ToDateTime(this string value, DateTime defaultValue = default(DateTime))
        {
            DateTime v;
            if (DateTime.TryParse(value, out v))
                return v;
            return defaultValue;
        }
      
        /// <summary>
        /// 将字符串转换为decimal类型。
        /// </summary>
        /// <param name="value">字符串。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static decimal ToDecimal(this string value, decimal defaultValue = default(decimal))
        {
            decimal v;
            if (decimal.TryParse(value, out v))
                return v;
            return defaultValue;
        }
     
        /// <summary>
        /// 将字符串转换为double类型。
        /// </summary>
        /// <param name="value">字符串。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static double ToDouble(this string value, double defaultValue = default(double))
        {
            double v;
            if (double.TryParse(value, out v))
                return v;
            return defaultValue;
        }
     
        /// <summary>
        /// 将字符串转换为float类型。
        /// </summary>
        /// <param name="value">字符串。</param>
        /// <param name="defaultValue">如果转换失败，返回默认值。</param>
        /// <returns>返回转换后的值。</returns>
        public static float ToSingle(this string value, float defaultValue = default(float))
        {
            float v;
            if (float.TryParse(value, out v))
                return v;
            return defaultValue;
        }

        /// <summary>
        /// 转换为GUID。
        /// </summary>
        /// <param name="value">要转换的对象实例。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回GUID实例对象。</returns>
        public static Guid ToGuid(this string value, Guid defaultValue = default(Guid))
        {
            try
            {
                Guid retValue;
                if (Guid.TryParse(value, out retValue))
                    return retValue;
            }
            catch { }
            return defaultValue;
        }

        /// <summary>
        /// 获取编码。
        /// </summary>
        /// <param name="value">编码对象。</param>
        /// <param name="encoding">编码默认值。</param>
        /// <returns>返回编码实例对象。</returns>
        public static Encoding ToEncoding(this string value, Encoding defaultValue = null)
        {
            if (defaultValue == null)
                defaultValue = Encoding.UTF8;
            try
            {
                return Encoding.GetEncoding(value);
            }
            catch { }
            return defaultValue;
        }

        /// <summary>
        /// 获取数据库返回的总记录数，一般在查询字符串后面加一句“SELECT COUNT(*) FROM ...”。
        /// </summary>
        /// <param name="reader"><see cref="T:System.Data.IDataReader"/>对象。</param>
        /// <param name="sizeName">总数量的列名。</param>
        /// <returns>返回数据库返回的总记录数。</returns>
        public static int GetSize(this IDataReader reader, string sizeName = null)
        {
            if (reader.NextResult() && reader.Read())
                return sizeName == null ? reader.GetInt32(0) : reader.GetInt32(sizeName);
            return 0;
        }

        #region boolean
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static bool GetBoolean(this IDataRecord dr, string name, bool defaultValue = false)
        {
            return dr.TConvert<bool>(name, defaultValue, o => { return Convert.ToBoolean(o); });
        }
        #endregion

        #region byte
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static byte GetByte(this IDataRecord dr, string name, byte defaultValue = default(byte))
        {
            return dr.TConvert<byte>(name, defaultValue, o => { return Convert.ToByte(o); });
        }
        #endregion

        #region sbyte
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static sbyte GetBytes(this IDataRecord dr, string name, sbyte defaultValue = default(sbyte))
        {
            return dr.TConvert<sbyte>(name, defaultValue, o => { return Convert.ToSByte(o); });
        }
        #endregion

        #region char
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static char GetChar(this IDataRecord dr, string name, char defaultValue = default(char))
        {
            return dr.TConvert<char>(name, defaultValue, o => { return Convert.ToChar(o); });
        }
        #endregion

        #region datetime
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static DateTime GetDateTime(this IDataRecord dr, string name, DateTime defaultValue = default(DateTime))
        {
            return dr.TConvert<DateTime>(name, defaultValue, o => { return Convert.ToDateTime(o); });
        }
        #endregion

        #region decimal
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static decimal GetDecimal(this IDataRecord dr, string name, decimal defaultValue = default(decimal))
        {
            return dr.TConvert<decimal>(name, defaultValue, o => { return Convert.ToDecimal(o); });
        }
        #endregion

        #region double
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static double GetDouble(this IDataRecord dr, string name, double defaultValue = default(double))
        {
            return dr.TConvert<double>(name, defaultValue, o => { return Convert.ToDouble(o); });
        }
        #endregion

        #region float
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static float GetFloat(this IDataRecord dr, string name, float defaultValue = default(float))
        {
            return dr.TConvert<float>(name, defaultValue, o => { return Convert.ToSingle(o); });
        }
        #endregion

        #region guid
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static Guid GetGuid(this IDataRecord dr, string name, Guid defaultValue = default(Guid))
        {
            return dr.TConvert<Guid>(name, defaultValue, o =>
            {
                try
                {
                    return (Guid)o;
                }
                catch { return new Guid(o.ToString()); }
            });
        }
        #endregion

        #region int16
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static short GetInt16(this IDataRecord dr, string name, short defaultValue = 0)
        {
            return dr.TConvert<short>(name, defaultValue, o => { return Convert.ToInt16(o); });
        }
        #endregion

        #region int32
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static int GetInt32(this IDataRecord dr, string name, int defaultValue = 0)
        {
            return dr.TConvert<int>(name, defaultValue, o => { return Convert.ToInt32(o); });
        }
        #endregion

        #region int64
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static long GetInt64(this IDataRecord dr, string name, long defaultValue = default(long))
        {
            return dr.TConvert<long>(name, defaultValue, o => { return Convert.ToInt64(o); });
        }
        #endregion

        #region uint16
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static ushort GetUInt16(this IDataRecord dr, string name, ushort defaultValue = 0)
        {
            return dr.TConvert<ushort>(name, defaultValue, o => { return Convert.ToUInt16(o); });
        }
        #endregion

        #region uint32
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static uint GetUInt32(this IDataRecord dr, string name, uint defaultValue = 0)
        {
            return dr.TConvert<uint>(name, defaultValue, o => { return Convert.ToUInt32(o); });
        }
        #endregion

        #region uint64
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static ulong GetUInt64(this IDataRecord dr, string name, ulong defaultValue = default(ulong))
        {
            return dr.TConvert<ulong>(name, defaultValue, o => { return Convert.ToUInt64(o); });
        }
        #endregion

        #region string
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回该类的值。</returns>
        public static string GetString(this IDataRecord dr, string name, string defaultValue = null)
        {
            return dr.TConvert<string>(name, defaultValue, o => { return Convert.ToString(o); });
        }
        #endregion

        #region general
        /// <summary>
        /// 通用获取数据库数据。
        /// </summary>
        /// <typeparam name="T">当前对象类型。</typeparam>
        /// <param name="dr">数据库读取器。</param>
        /// <param name="name">列名，不能为空。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回对象实例。</returns>
        public static T TGet<T>(this IDataReader dr, string name, T defaultValue = default(T))
        {
            if (name == null)
            {
                try { return (T)Activator.CreateInstance(typeof(T), new object[] { dr }); }
                catch
                {
                    try
                    {
                        if (typeof(T).IsSubclassOf(typeof(BaseObject)))
                        {
                            var obj = Activator.CreateInstance(typeof(T)) as BaseObject;
                            obj.LoadFromDataReader(dr);
                            return (T)(object)obj;
                        }
                    }
                    catch { }
                    return defaultValue;
                }
            }
            return dr.TConvert<T>(name, defaultValue, value =>
            {
                return value.ChangeTo<T>(defaultValue);
            });
        }
        /// <summary>
        /// 从数据库读取器中获取对象。
        /// </summary>
        /// <typeparam name="T">当前对象类型。</typeparam>
        /// <param name="dr">数据库读取实例。</param>
        /// <param name="closed">是否关闭当前读取实例对象。</param>
        /// <returns>返回实例对象。</returns>
        public static T TGet<T>(this IDataReader dr, bool closed = true) where T : class, new()
        {
            T value = default(T);
            if (dr.Read())
                value = dr.TGet<T>(null);
            if (closed)
                dr.Close();
            return value;
        }
        /// <summary>
        /// 从数据库读取器中获取对象列表。
        /// </summary>
        /// <typeparam name="T">当前对象类型。</typeparam>
        /// <param name="dr">数据库读取实例。</param>
        /// <param name="closed">是否关闭当前读取实例对象。</param>
        /// <returns>返回实例对象列表。</returns>
        public static List<T> TGetList<T>(this IDataReader dr, bool closed = true) where T : class, new()
        {
            List<T> list = new List<T>();
            while (dr.Read())
            {
                list.Add(dr.TGet<T>(null));
            }
            if (closed)
                dr.Close();
            return list;
        }
        /// <summary>
        /// 从数据库读取器中获取对象列表（分页显示使用）。
        /// </summary>
        /// <typeparam name="T">当前对象类型。</typeparam>
        /// <param name="dr">数据库读取实例。</param>
        /// <param name="size">总记录数。</param>
        /// <param name="closed">是否关闭当前读取实例对象。</param>
        /// <returns>返回实例对象列表。</returns>
        public static List<T> TGetList<T>(this IDataReader dr, out int size, bool closed = true) where T : class, new()
        {
            List<T> list = new List<T>();
            while (dr.Read())
            {
                list.Add(dr.TGet<T>(null));
            }
            size = dr.GetSize();
            if (closed)
                dr.Close();
            return list;
        }
        #endregion

        #region helper
        private static T TConvert<T>(this IDataRecord dr, string name, T defaultValue, Func<object, T> converter)
        {
            try
            {
                object value = dr[name];
                if (value == DBNull.Value)
                    return defaultValue;
                return converter(value);
            }
            catch { }
            return defaultValue;
        }
        #endregion

        #region encoding
        /// <summary>
        /// 获取数据库中列值。
        /// </summary>
        /// <param name="dr"><see cref="T:System.Data.IDataRecord"/>实例对象。</param>
        /// <param name="name">列名称。</param>
        /// <param name="defaultValue">默认值，如果为空默认为UTF-8。</param>
        /// <returns>返回该类的值。</returns>
        public static Encoding GetEncoding(this IDataRecord dr, string name, Encoding defaultValue = null)
        {
            if (defaultValue == null)
                defaultValue = Encoding.UTF8;
            return dr.TConvert<Encoding>(name, defaultValue, o => { return Encoding.GetEncoding(o as string); });
        }
        #endregion
    }
}